package com.zjitc.config;

import com.zaxxer.hikari.HikariDataSource;
import com.zjitc.mapper.UserMapper;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/8 16:24
 * @description:
 */
@Configuration
@ComponentScan(basePackages = "com.zjitc.config")
public class JavaConfigDome2 {
  @Bean(destroyMethod = "close")
  public HikariDataSource dataSource(JavaConfigDome javaConfigDome)
  {
    HikariDataSource source=new HikariDataSource();
    source.setDriverClassName(javaConfigDome.getDriverClass());
    source.setJdbcUrl(javaConfigDome.getUrl());
    source.setPassword(javaConfigDome.getPassword());
    source.setUsername(javaConfigDome.getUsername());
    return source;
  }
  @Bean
  public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource source)
  {
    SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(source);
    return sqlSessionFactoryBean;
  }
  @Bean
  public MapperFactoryBean<UserMapper> mapperMapperFactoryBean(SqlSessionFactory sqlSessionFactory)
  {
    MapperFactoryBean<UserMapper> userMapperMapperFactoryBean=new MapperFactoryBean<UserMapper>();
    userMapperMapperFactoryBean.setMapperInterface(UserMapper.class);
    userMapperMapperFactoryBean.setSqlSessionFactory(sqlSessionFactory);
    return userMapperMapperFactoryBean;
  }

}
